FROM ubuntu:20.04

ARG ARCH
ARG GAUSSHOME=/usr/local/mogdb

ENV LANG en_US.utf8
ENV GAUSSHOME $GAUSSHOME
ENV PGDATA /var/lib/mogdb/data

RUN set -eux; \
    if [ "$ARCH" = "arm64" ]; then \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse" >/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse" >>/etc/apt/sources.list; \
    elif [ "$ARCH" = "amd64" ]; then \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse" >/etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse" >>/etc/apt/sources.list; \
    fi && \
    apt-get update && apt-get install -y \
    ca-certificates \
    iputils-ping \
    libncurses5 \
    ncat \
    libaio-dev \
    bzip2 \
    libkeyutils-dev \
    locales \
    libnuma-dev \
    libreadline-dev \
    vim  \
    procps && \
    groupadd --gid=70 omm && \
    useradd -g omm --uid=70 --home-dir=/home/omm --create-home --shell=/bin/bash omm && \
    mkdir /docker-entrypoint-initdb.d && chown omm:omm /docker-entrypoint-initdb.d && \
    mkdir -p /var/lib/mogdb && chown omm:omm /var/lib/mogdb && \
    mkdir -p /var/run/mogdb && chown omm:omm /var/run/mogdb && \
    sed -i 's/^# *\(en_US.UTF-8\)/\1/' /etc/locale.gen && locale-gen && \
    sed -i 's/^# *\(en_US.UTF-8\)/\1/' /etc/locale.gen && locale-gen && \
    echo "export GAUSSHOME=/usr/local/mogdb"  >> /home/omm/.profile && \
    echo "export PATH=\$GAUSSHOME/bin:\$PATH " >> /home/omm/.profile && \
    echo "export LD_LIBRARY_PATH=\$GAUSSHOME/lib:\$LD_LIBRARY_PATH" >> /home/omm/.profile && \
    echo "export GAUSSLOG=/home/omm/pg_log" >> /home/omm/.profile && \
    echo "export PGDATA=/var/lib/mogdb/data" >> /home/omm/.profile && \
    echo "\set PROMPT1 'MogDB%R%#'" >> /home/omm/.gsqlrc && \
    echo "\set PROMPT2 '#'" >> /home/omm/.gsqlrc && \
    echo "\set PROMPT3 '>'" >> /home/omm/.gsqlrc && \
    sed -i -E 's/# (en_US.UTF-8 UTF-8)/\1/' /etc/locale.gen && \
    sed -i -E 's/# (zh_CN.UTF-8 UTF-8)/\1/' /etc/locale.gen && \
    sed -i -E 's/# (zh_CN.GBK GBK)/\1/' /etc/locale.gen && \
    locale-gen

COPY base/entrypoint.sh /usr/local/bin/